package com.atguigu.gmallsugar.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmallsugar.bean.OrderRefund;
import com.atguigu.gmallsugar.bean.Province;
import com.atguigu.gmallsugar.bean.Spu;
import com.atguigu.gmallsugar.bean.Tm;
import com.atguigu.gmallsugar.mapper.TradeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author lzc
 * @Date 2023/8/14 14:37
 */
@Service
public class TradeServiceImpl implements TradeService{
    
    @Autowired
    TradeMapper tradeMapper;
    @Override
    public String gmv(Integer date) {
        BigDecimal gmv = tradeMapper.gmv(date);
        JSONObject obj = new JSONObject();
    
        obj.put("status", 0);
        obj.put("msg", "");
        obj.put("data", gmv);
        
        return obj.toJSONString();
    }
    
    @Override
    public String gmvBySpu(Integer date) {
    
        List<Spu> list = tradeMapper.gmvBySpu(date);
    
        JSONObject result = new JSONObject();
    
    
    
        result.put("status", 0);
        result.put("msg", "");
    
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (Spu spu : list) {
            categories.add(spu.getSpu_name());
        }
        data.put("categories", categories);
    
        JSONArray series = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.put("name", "商品的 spu");
        JSONArray data1 = new JSONArray();
        for (Spu spu : list) {
            data1.add(spu.getOrder_amount());
        }
        obj.put("data", data1);
        series.add(obj);
        
        data.put("series", series);
    
        result.put("data", data);
        return result.toJSONString();
    }
    
    @Override
    public String gmvByTm(Integer date) {
        JSONObject result = new JSONObject();
    
    
        List<Tm> list = tradeMapper.gmvByTm(date);
        
        result.put("status", 0);
        result.put("msg", "");
    
        JSONArray data = new JSONArray();
        for (Tm tm : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", tm.getTrademark_name());
            obj.put("value", tm.getOrder_amount());
    
            data.add(obj);
        }
    
       
    
        result.put("data", data);
        return result.toJSONString();
    }
    
    @Override
    public String category3Stat(Integer date) {
    
        List<OrderRefund> list = tradeMapper.category3Stat(date);
    
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
    
        JSONObject data = new JSONObject();
    
        JSONArray columns = new JSONArray();
        
        JSONObject column1 = new JSONObject();
        column1.put("name", "3级品类");
        column1.put("id", "c3");
        columns.add(column1);
        
        JSONObject column2 = new JSONObject();
        column2.put("name", "下单金额");
        column2.put("id", "order_amount");
        columns.add(column2);
        
        JSONObject column3 = new JSONObject();
        column3.put("name", "退单金额");
        column3.put("id", "refund_amount");
        columns.add(column3);
        
        JSONObject column4 = new JSONObject();
        column4.put("name", "退单次数");
        column4.put("id", "refund_count");
        columns.add(column4);
        
        JSONObject column5 = new JSONObject();
        column5.put("name", "退单人数");
        column5.put("id", "refund_user_count");
        columns.add(column5);
        
        data.put("columns", columns);
    
        JSONArray rows = new JSONArray();
    
        for (OrderRefund or : list) {
            JSONObject row = new JSONObject();
            row.put("c3", or.getCategory3_name());
            row.put("order_amount", or.getOrder_amount());
            row.put("refund_amount", or.getRefund_amount());
            row.put("refund_count", or.getRefund_count());
            row.put("refund_user_count", or.getRefund_user_count());
            rows.add(row);
        }
        data.put("rows", rows);
    
        result.put("data", data);
        return result.toJSONString();
    }
    
    @Override
    public String province(Integer date) {
        JSONObject result = new JSONObject();
    
    
        List<Province> list = tradeMapper.province(date);
    
        result.put("status", 0);
        result.put("msg", "");
    
        JSONObject data = new JSONObject();
        data.put("valueName", "下单金额");
        
        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("订单数");
        data.put("tooltipNames", tooltipNames);
        
        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("个");
        data.put("tooltipUnits", tooltipUnits);
    
    
        JSONArray mapData = new JSONArray();
        for (Province province : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", province.getProvince_name());
            obj.put("value", province.getOrder_amount());
    
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(province.getOrder_count());
            obj.put("tooltipValues", tooltipValues);
            
            mapData.add(obj);
        }
        data.put("mapData", mapData);
    
        result.put("data", data);
        return result.toJSONString();
    }
    
}
